﻿using BaseProject.Application.Interfaces;
using BaseProject.Domain.Models;
using BaseProject.Domain.Repositories;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace BaseProject.Application.Implementation
{
    public class BaseServiceImpl<T> : IBaseService<T> where T : BaseEntity
    {
        private readonly IBaseRepository<T> _baseRepository;

        public BaseServiceImpl(IBaseRepository<T> baseRepository)
        {
            this._baseRepository = baseRepository;
        }

        public virtual async Task<bool> Insert(T t)
        {
            return await this._baseRepository.Insert(t);
        }

        public virtual async Task<bool> Update(T t)
        {
            return await this._baseRepository.Update(t);
        }

        public virtual async Task<bool> Delete(long id)
        {
            return await this._baseRepository.Delete(id);
        }

        public virtual async Task<T> Get(long id)
        {
            return await _baseRepository.GetById(id);
        }

        public virtual async Task<IEnumerable<T>> GetList()
        {
            return await this._baseRepository.GetList();
        }

        public virtual async Task<IEnumerable<T>> GetPagedList(int pageNumber, int pageSize, string conditions, string orderby, object param)
        {
            return await this._baseRepository.GetPagedList(pageNumber, pageSize, conditions, orderby, param);
        }

        public virtual async Task<int> RecordCount(string conditions, object param)
        {
            return await this._baseRepository.RecordCount(conditions, param);
        }

        public virtual async Task<IEnumerable<T>> Query(string sql, object param)
        {
            return await this._baseRepository.Query(sql, param);
        }

        public virtual async Task<int> ExecuteScalar(string sql, object param)
        {
            return await this._baseRepository.ExecuteScalar(sql, param);
        }

        public virtual async Task<T> QueryFirst(string sql, object param)
        {
            return await this._baseRepository.QueryFirst(sql, param);
        }

        public virtual async Task<int> GetMaxId(string tableName)
        {
            return await this._baseRepository.ExecuteScalar("SELECT max(id) from " + tableName, null);
        }
    }
}